home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / misc / aterminfo.lha / comp_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  2.9 KB  |  157 lines

  1.  
  2. /* This work is copyrighted. See COPYRIGHT.OLD & COPYRIGHT.NEW for   *
  3. *  details. If they are missing then this copy is in violation of    *
  4. *  the copyright conditions.                                        */
  5.  
  6. /*
  7.  *    comp_main.c --- Main program for terminfo compiler
  8.  *
  9.  */
  10.  
  11. #include <stdlib.h>
  12. /*#include <unistd.h>*/
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include "compiler.h"
  16.  
  17. long    start_time;        /* time at start of compilation */
  18.  
  19. FILE    *curr_fh;
  20.  
  21. int    curr_line;        /* current line # in input */
  22. long    curr_file_pos;        /* file offset of current line */
  23.  
  24. int    debug_level;        /* level of debugging output */
  25.  
  26. struct token    curr_token;
  27.  
  28. char    *source_file = "terminfo";
  29. char    *destination = SRCDIR;
  30. char    *usage_string = "\ttic [-v[n]] source-file\n";
  31. char    check_only = 0;
  32.  
  33. extern void init(char *);
  34. extern void make_hash_table(void);
  35. extern void compile(void);
  36.  
  37. int main (argc, argv)
  38. int    argc;
  39. char    *argv[];
  40. {
  41.     int    i;
  42.     int    argflag = FALSE;
  43.  
  44.     debug_level = 0;
  45.  
  46.     for (i=1; i < argc; i++)
  47.     {
  48.         if (argv[i][0] == '-')
  49.         {
  50.         switch (argv[i][1])
  51.         {
  52.             case 'c':
  53.             check_only = 1;
  54.             break;
  55.  
  56.             case 'v':
  57.             debug_level = argv[i][2]  ?  atoi(&argv[i][2])  :  1;
  58.             break;
  59.  
  60.             default:
  61.             fprintf(stderr, "%s: Unknown option. Usage is:\n\t%s\n",
  62.                                argv[0], usage_string);
  63.             exit(1);
  64.         }
  65.         }
  66.         else if (argflag)
  67.         {
  68.         fprintf(stderr, "%s: Too many file names.  Usage is:\n\t%s\n",
  69.                             argv[0], usage_string);
  70.         exit(1);
  71.         }
  72.         else
  73.         {
  74.         argflag = TRUE;
  75.         source_file = argv[i];
  76.         }
  77.     }
  78.  
  79.     init(argv[0]);
  80.     make_hash_table();
  81.     compile();
  82.  
  83.     return(0);
  84. }
  85.  
  86.  
  87. /*
  88.  *    init(progname)
  89.  *
  90.  *    Miscelaneous initialisations
  91.  *
  92.  *    Open source file as standard input
  93.  *    Check for access rights to destination directories
  94.  *    Create any directories which don't exist.
  95.  *
  96.  */
  97.  
  98. void init(progname)
  99. char    *progname;
  100. {
  101.     struct stat    statbuf;
  102.     char        *dirnames = "abcdefghijklmnopqrstuvwxyz0123456789";
  103.     char        *getenv();
  104.     char        dir[2];
  105.  
  106.     start_time = time(0);
  107.  
  108.     curr_line = 0;
  109.  
  110.     if ((curr_fh = fopen(source_file, "r")) == NULL)
  111.     {
  112.         fprintf(stderr, "%s: Can't open %s\n", progname, source_file);
  113.         exit(1);
  114.     }
  115.  
  116.     if (getenv("TERMINFO") != NULL)
  117.         destination = getenv("TERMINFO");
  118.  
  119.     if (access(destination, 7) < 0)
  120.     {
  121.         fprintf(stderr, "%s: %s non-existant or permission denied\n",
  122.                             progname, destination);
  123.         exit(1);
  124.     }
  125.  
  126.     if (chdir(destination) < 0)
  127.     {
  128.         fprintf(stderr, "%s: %s is not a directory\n",
  129.                             progname, destination);
  130.         exit(1);
  131.     }
  132.  
  133.     dir[1] = '\0';
  134.     for (dir[0] = *dirnames; *dirnames != '\0'; dir[0] = *(++dirnames))
  135.     {
  136.         if (stat(dir, &statbuf) < 0)
  137.         {
  138.         mkdir(dir, 0755);
  139.         }
  140.         else if (access(dir, 7) < 0)
  141.         {
  142.         fprintf(stderr, "%s: %s/%s: Permission denied\n",
  143.                             progname, destination, dir);
  144.         exit(1);
  145.         }
  146.         else if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
  147.         {
  148.         fprintf(stderr, "%s: %s/%s: Not a directory\n",
  149.                             progname, destination, dir);
  150.        exit(1);
  151.         }
  152.     }
  153.  
  154. }
  155.  
  156.  
  157.